package com.example.exceldemo.util;

import com.example.exceldemo.exception.BizException;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 配置工具类
 *
 * @author system
 */
@Slf4j
public class ConfigUtil {

    /**
     * 私有构造方法，防止实例化
     */
    private ConfigUtil() {
        throw new IllegalStateException("工具类不能实例化");
    }

    /**
     * 读取配置文件
     *
     * @param configFilePath 配置文件路径
     * @return 配置信息
     * @throws IOException IO异常
     */
    public static Map<String, String> readConfig(String configFilePath) throws IOException {
        if (configFilePath == null || configFilePath.trim().isEmpty()) {
            throw new IllegalArgumentException("配置文件路径不能为空");
        }
        
        File configFile = new File(configFilePath);
        Map<String, String> configMap = new HashMap<>(16);
        
        if (!configFile.exists()) {
            log.info("配置文件不存在: {}, 将返回空配置", configFilePath);
            return configMap;
        }
        
        Properties properties = new Properties();
        try (FileInputStream fis = new FileInputStream(configFile)) {
            properties.load(fis);
            
            for (String key : properties.stringPropertyNames()) {
                configMap.put(key, properties.getProperty(key));
            }
            
            log.info("成功读取配置文件: {}, 包含 {} 个配置项", configFilePath, configMap.size());
        }
        
        return configMap;
    }
    
    /**
     * 保存配置文件
     *
     * @param configFilePath 配置文件路径
     * @param configMap 配置信息
     * @throws IOException IO异常
     */
    public static void saveConfig(String configFilePath, Map<String, String> configMap) throws IOException {
        if (configFilePath == null || configFilePath.trim().isEmpty()) {
            throw new IllegalArgumentException("配置文件路径不能为空");
        }
        
        if (configMap == null) {
            throw new IllegalArgumentException("配置信息不能为空");
        }
        
        // 确保目录存在
        createParentDirectories(configFilePath);
        
        Properties properties = new Properties();
        
        for (Map.Entry<String, String> entry : configMap.entrySet()) {
            if (entry.getKey() != null && entry.getValue() != null) {
                properties.setProperty(entry.getKey(), entry.getValue());
            }
        }
        
        try (FileOutputStream fos = new FileOutputStream(configFilePath)) {
            properties.store(fos, "DocGen Configuration - " + System.currentTimeMillis());
            log.info("成功保存配置文件: {}, 包含 {} 个配置项", configFilePath, configMap.size());
        }
    }
    
    /**
     * 创建父目录
     *
     * @param filePath 文件路径
     * @throws IOException IO异常
     */
    private static void createParentDirectories(String filePath) throws IOException {
        File file = new File(filePath);
        File parentDir = file.getParentFile();
        
        if (parentDir != null && !parentDir.exists()) {
            boolean created = parentDir.mkdirs();
            if (!created) {
                throw new IOException("无法创建目录: " + parentDir.getAbsolutePath());
            }
            log.info("创建目录: {}", parentDir.getAbsolutePath());
        }
    }
} 